package com.veo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.veo.entity.Admin;
import com.veo.entity.User;
import com.veo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;


    @RequestMapping("login")
    public String toLogin(){
        return "admin/login";
    }

    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("admin");
        return "admin/login";
    }

    @RequestMapping("doLogin")
    @ResponseBody
    public Map<String, Object> doLogin(Admin admin, HttpSession session){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>().eq("name", admin.getName());
        Admin one = adminService.getOne(queryWrapper);
        Map<String,Object> map = new HashMap<>();
        if (one == null){
            map.put("msg","error");
            return map;
        }else {
            //判断密码
            if (admin.getPassword().equals(one.getPassword())){
                System.out.println("--------------------->" + one);
                map.put("msg","ok");
                session.setAttribute("admin",one);
                return map;
            }else {
                map.put("msg","error");
                return map;
            }
        }
    }

}
